package com.rdk.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class RelativeDateFormat {

    private static final long ONE_MINUTE = 60000L;
    private static final long ONE_HOUR = 3600000L;
    private static final long ONE_DAY = 86400000L;
    private static final long ONE_WEEK = 604800000L;

    private static final String ONE_SECOND_AGO = "秒前";
    private static final String ONE_MINUTE_AGO = "分钟前";
    private static final String ONE_HOUR_AGO = "小时前";
    private static final String ONE_DAY_AGO = "天前";
    private static final String ONE_MONTH_AGO = "月前";
    private static final String ONE_YEAR_AGO = "年前";

    private static SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm");

    // 时间转换
    public static String format(Date date) {
        Calendar calendar = Calendar.getInstance(), cal = Calendar.getInstance();
        cal.setTime(date);
        // Date now = new Date();
        long delta = calendar.getTimeInMillis() - cal.getTimeInMillis();
        if (delta < 1L * ONE_MINUTE) {
            long seconds = toSeconds(delta);
            System.out.println(seconds);
            if(seconds > 1){
            	return (seconds <= 0 ? 1 : seconds) + ","+ONE_SECOND_AGO+",1";
            }
            return (seconds <= 0 ? 1 : seconds) + ","+ONE_SECOND_AGO+",2";
        }
        if (delta < 45L * ONE_MINUTE) {
            long minutes = toMinutes(delta);
            System.out.println(minutes);
            if(minutes > 1){
            	return (minutes <= 0 ? 1 : minutes) + ","+ONE_MINUTE_AGO+",3";
            }
            return (minutes <= 0 ? 1 : minutes) + ","+ONE_MINUTE_AGO+",4";
        }
        if (calendar.get(Calendar.YEAR) == cal.get(Calendar.YEAR)
                && calendar.get(Calendar.MONTH) == cal.get(Calendar.MONTH)
                && calendar.get(Calendar.DATE) == cal.get(Calendar.DATE)) {
            // if (delta < 24L * ONE_HOUR) {
            long hours = toHours(delta);
            // return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO;
            return format.format(cal.getTime()).substring(5)+",今天,5";
        }
        calendar.add(Calendar.DATE, -1);
        if (calendar.get(Calendar.YEAR) == cal.get(Calendar.YEAR)
                && calendar.get(Calendar.MONTH) == cal.get(Calendar.MONTH)
                && calendar.get(Calendar.DATE) == cal.get(Calendar.DATE)) {
            // if (delta < 48L * ONE_HOUR) {
            return format.format(cal.getTime()).substring(5)+",昨天,6";
        } else {
            return format.format(cal.getTime());
        }
        // if (delta < 30L * ONE_DAY) {
        // long days = toDays(delta);
        // return (days <= 0 ? 1 : days) + ONE_DAY_AGO;
        // }
        // if (delta < 12L * 4L * ONE_WEEK) {
        // long months = toMonths(delta);
        // return (months <= 0 ? 1 : months) + ONE_MONTH_AGO;
        // } else {
        // long years = toYears(delta);
        // return (years <= 0 ? 1 : years) + ONE_YEAR_AGO;
        // }
    }

    private static long toSeconds(long date) {
        return date / 1000L;
    }

    private static long toMinutes(long date) {
        return toSeconds(date) / 60L;
    }

    private static long toHours(long date) {
        return toMinutes(date) / 60L;
    }

    private static long toDays(long date) {
        return toHours(date) / 24L;
    }

    private static long toMonths(long date) {
        return toDays(date) / 30L;
    }

    private static long toYears(long date) {
        return toMonths(date) / 365L;
    }

}
